package com.geovis.emergency.spd.controller.command;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillEarlyDisposeService;
import com.geovis.emergency.spd.biz.system.service.ISystemFileService;
import com.geovis.emergency.spd.cache.DictCache;
import com.geovis.emergency.spd.entity.command.pojo.vo.CommandDrillEarlyDisposeVO;
import com.geovis.emergency.spd.entity.command.pojo.vo.CommandDrillPublishInfoVO;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillEarlyDispose;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPublishInfo;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcDisasterConstant;
import com.geovis.emergency.spd.entity.system.constant.SystemFileBusinessConstant;
import com.geovis.emergency.spd.entity.system.entity.SystemDictData;
import com.geovis.emergency.spd.entity.system.entity.SystemFile;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemFileDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemFileQueryDTO;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/command/drill/early/dispose")
@RequiredArgsConstructor
@Api(value = "指挥调度大屏-前置处置接口", tags = "指挥调度大屏-前置处置接口")
@Slf4j
@Validated
@ApiSort(5)
public class CommandDrillEarlyDisposeController extends BaseController<IPcDrillEarlyDisposeService> {
    private final ISystemFileService systemFileService;
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="appId",dataType="String",required=true,value="授权id"),
            @ApiImplicitParam(paramType="header",name="timestamp",dataType="String",required=true,value="当前时间戳(秒)"),
            @ApiImplicitParam(paramType="header",name="sign",dataType="String",required=true,value="签名"),
            @ApiImplicitParam(paramType="path",name="mainEventId",dataType="String",required=true,value="演练主事件id")
    })
    @ApiOperation(value = "处置列表", notes = "处置列表，签名生成方法：md5(appId+appSecret+timestamp)")
    @GetMapping("/list/{mainEventId}")
    public Result<List<CommandDrillEarlyDisposeVO>> list(@PathVariable("mainEventId") String mainEventId) {
        List<PcDrillEarlyDispose> list = baseService.list(
                Wrappers.lambdaQuery(PcDrillEarlyDispose.class)
                        .eq(PcDrillEarlyDispose::getEventId,mainEventId)
                        .orderByDesc(PcDrillEarlyDispose::getDisTime)
        );
        List<CommandDrillEarlyDisposeVO> collect = list.stream().map(dispose -> {
            CommandDrillEarlyDisposeVO commandDrillEarlyDisposeVO = BeanUtil.toBean(dispose, CommandDrillEarlyDisposeVO.class);
            //获取附件信息
            SystemFileQueryDTO systemFileQueryDTO = new SystemFileQueryDTO();
            systemFileQueryDTO.setRelatedId(dispose.getId());
            systemFileQueryDTO.setType(SystemFileBusinessConstant.TYPE_DRILL_EARLY_DISPOSE);
            List<SystemFileDTO> fileDTOList = systemFileService.getList(systemFileQueryDTO);
            commandDrillEarlyDisposeVO.setFileList(fileDTOList.stream().map(s -> BeanUtil.toBean(s, SystemFile.class)).collect(Collectors.toList()));
            return commandDrillEarlyDisposeVO;
        }).collect(Collectors.toList());
        return Result.ok(collect);
    }
}
